﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonClasses
{
    public class Log
    {
        //school
        string connectionString = @"Data Source=t-sql;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
       
        private int _empID;
        private string _appmsg;
        private string _exceptmsg;
        private string _severity;

        public int EmpID { get { return _empID; } set { this._empID = value; } }
        public string Appmsg { get { return _appmsg; } set { this._appmsg = value; } }
        public string ExceptMsg { get { return _exceptmsg; } set { this._exceptmsg = value; } }
        public string Severity { get { return _severity; } set { this._severity = value; } }

      
        public string LogTransaction(int eid, string appmsg, string exceptmsg, string severity)
        {
            string results = "";
            string compname = System.Environment.MachineName;  //gets name of local computer
            DateTime date = DateTime.Now; //time stamp
            string source = "testing"; //plant? from group?

            _empID = eid;
            _appmsg = appmsg;
            _exceptmsg = exceptmsg;
            _severity = severity;

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                using (SqlCommand insertCommand = new SqlCommand(
                "INSERT INTO [ApplicationLog].[dbo].[ApplicationLog] VALUES(@EmpID, @ComputerName,@Date, @Source, @ApplicationMsg, @ExceptionMsg,@Severity)", connection))
                {
                    insertCommand.Parameters.AddWithValue("@EmpID", _empID);
                    insertCommand.Parameters.AddWithValue("@ComputerName", compname);
                    insertCommand.Parameters.AddWithValue("@Date", date);
                    insertCommand.Parameters.AddWithValue("@Source", source);
                    insertCommand.Parameters.AddWithValue("@ApplicationMsg", _appmsg);
                    insertCommand.Parameters.AddWithValue("@exceptionMsg", _exceptmsg);
                    insertCommand.Parameters.AddWithValue("@Severity", _severity);

                    insertCommand.Connection = connection;
                    connection.Open();
                    insertCommand.ExecuteNonQuery();
                    results = "Success! Event Logged";
                }

            }//end try
            catch (System.Data.SqlClient.SqlException sqlException)
            {
                results = sqlException.Message;
            }
            return results;
        }
    }
}
